package org.lotic.ldk.object;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TypeTransition {
	private static final Log log = LogFactory.getLog(TypeTransition.class);

	private static String[] types = { "byte", "short", "int", "long", "float",
			"double", "char", "boolan", "java.lang.String"};

	/**
	 * 将字符串值转换指定目标类型.
	 * 
	 * @param value
	 * @param targetType
	 * @return
	 */
	public static final Object typeTransiton(String value, Class targetType) {
		String type = targetType.getName();
		switch (index(type)) {
		case 0:
			return Byte.parseByte(value);
		case 1:
			return Short.parseShort(value);
		case 2:
			return Integer.parseInt(value);
		case 3:
			return	Long.parseLong(value);

		case 4:
			return Float.parseFloat(value);
		case 5:
			return Double.parseDouble(value);
		case 6:	
			
			return value.charAt(0);
		case 7:
			return Boolean.parseBoolean(value);
		case 8:
			return value;
			
		default:
			log.info(type + "类型无法转换");
			break;
		}
		return null;
	}

	private static int index(String type) {
		for (int i = 0; i < types.length; i++) {
			if (type.equals(types[i])) {
				return i;
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		System.out.println(byte.class.getName());
		System.out.println(short.class.getName());
		System.out.println(int.class.getName());
		System.out.println(long.class.getName());
		System.out.println(float.class.getName());
		System.out.println(double.class.getName());
		System.out.println(char.class.getName());
		System.out.println(boolean.class.getName());
		System.out.println(String.class.getName());

	}

}
